#include <iostream>
#include <cstdio>
#include <vector>

using std::vector;
using std::cin;
using std::cout;
using std::min;

void input(vector <vector <int> > &cost, vector <vector <int> > &dp) {
    int n, m;
    cin >> n >> m;
    cost.resize(n);
    dp.resize(n);
    for (int i = 0; i < n; i++) {
        dp[i].resize(m);
        cost[i].resize(m);
        for (int j = 0; j < m; j++) {
            cin >> cost[i][j];
        }
    }
}

void solution(const vector <vector <int> > &cost, vector <vector <int> > &dp) {
    dp[0][0] = cost[0][0];
    for (int i = 1; i < cost.size(); i++) {
        dp[i][0] = dp[i - 1][0] + cost[i][0];
    }
    for (int i = 1; i < cost[0].size(); i++) {
        dp[0][i] = dp[0][i - 1] + cost[0][i];
    }
    for (int i = 1; i < cost.size(); i++) {
        for (int j = 1; j < cost[i].size(); j++) {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + cost[i][j];
        }
    }
}

void output(const vector <vector <int> > &dp) {
    cout << dp.back().back();
}

int main() {
    vector <vector <int> > cost, dp;
    input(cost, dp);
    solution(cost, dp);
    output(dp);
    return 0;
}
